package com.xiaoyi.intentsdklibrary.SDK.FloatView;

import android.content.Context;
import android.os.Build;
import android.os.Handler;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;
import com.xiaoyi.intentsdklibrary.Bean.TopClickType;
import com.xiaoyi.intentsdklibrary.Bean.TopClickTypeBean;
import com.lkx.liandianqinew.R;
import com.xiaoyi.intentsdklibrary.SDK.SDK;
import org.greenrobot.eventbus.EventBus;

public class TopViewSDK {
    private static final TopViewSDK ourInstance = new TopViewSDK();
    private Handler mHandler;
    private View mInflate;
    private TextView mNum;
    private WindowManager.LayoutParams mParams;
    private ImageView mPause;
    private ImageView mPlaying;
    private Runnable mR;
    private TextView mText;
    private WindowManager mWindowManager;

    public static TopViewSDK getInstance() {
        return ourInstance;
    }

    private TopViewSDK() {
    }

    public void init(Context context) {
        try {
            if (this.mWindowManager == null) {
                this.mWindowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
                View inflate = View.inflate(context, R.layout.top_view_layout, null);
                this.mInflate = inflate;
                this.mPause = (ImageView) inflate.findViewById(R.id.id_pause);
                this.mPlaying = (ImageView) this.mInflate.findViewById(R.id.id_playing);
                this.mText = (TextView) this.mInflate.findViewById(R.id.id_text);
                this.mNum = (TextView) this.mInflate.findViewById(R.id.id_num);
                ImageView imageView = (ImageView) this.mInflate.findViewById(R.id.id_exit);
                this.mPause.setOnClickListener(new View.OnClickListener() {
                    /* class com.xiaoyi.intentsdklibrary.SDK.FloatView.TopViewSDK.AnonymousClass1 */

                    public void onClick(View view) {
                        TopViewSDK.this.mPause.setVisibility(View.GONE);
                        TopViewSDK.this.mPlaying.setVisibility(View.VISIBLE);
                        EventBus.getDefault().post(new TopClickTypeBean(TopClickType.PAUSE, ""));
                    }
                });
                this.mPause.setOnLongClickListener(new View.OnLongClickListener() {
                    /* class com.xiaoyi.intentsdklibrary.SDK.FloatView.TopViewSDK.AnonymousClass2 */

                    public boolean onLongClick(View view) {
                        TopViewSDK.this.mPause.setVisibility(View.GONE);
                        TopViewSDK.this.mPlaying.setVisibility(View.VISIBLE);
                        EventBus.getDefault().post(new TopClickTypeBean(TopClickType.PAUSE, ""));
                        return true;
                    }
                });
                this.mPlaying.setOnClickListener(new View.OnClickListener() {
                    /* class com.xiaoyi.intentsdklibrary.SDK.FloatView.TopViewSDK.AnonymousClass3 */

                    public void onClick(View view) {
                        TopViewSDK.this.mPlaying.setVisibility(View.GONE);
                        TopViewSDK.this.mPause.setVisibility(View.VISIBLE);
                        EventBus.getDefault().post(new TopClickTypeBean(TopClickType.RESUME, ""));
                    }
                });
                this.mPlaying.setOnLongClickListener(new View.OnLongClickListener() {
                    /* class com.xiaoyi.intentsdklibrary.SDK.FloatView.TopViewSDK.AnonymousClass4 */

                    public boolean onLongClick(View view) {
                        TopViewSDK.this.mPlaying.setVisibility(View.GONE);
                        TopViewSDK.this.mPause.setVisibility(View.VISIBLE);
                        EventBus.getDefault().post(new TopClickTypeBean(TopClickType.RESUME, ""));
                        return true;
                    }
                });
                imageView.setOnClickListener(new View.OnClickListener() {
                    /* class com.xiaoyi.intentsdklibrary.SDK.FloatView.TopViewSDK.AnonymousClass5 */

                    public void onClick(View view) {
                        EventBus.getDefault().post(new TopClickTypeBean(TopClickType.EXIT, ""));
                    }
                });
                imageView.setOnLongClickListener(new View.OnLongClickListener() {
                    /* class com.xiaoyi.intentsdklibrary.SDK.FloatView.TopViewSDK.AnonymousClass6 */

                    public boolean onLongClick(View view) {
                        EventBus.getDefault().post(new TopClickTypeBean(TopClickType.EXIT, ""));
                        return true;
                    }
                });
                WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams();
                this.mParams = layoutParams;
                layoutParams.flags = 1416;
                this.mParams.format = 1;
                if (Build.VERSION.SDK_INT >= 26) {
                    this.mParams.type = 2038;
                } else {
                    this.mParams.type = 2003;
                }
                this.mParams.gravity = 8388659;
                this.mParams.x = 0;
                this.mParams.y = 0;
                this.mParams.width = -1;
                this.mParams.height = dip2px(context, 40.0f);
                this.mWindowManager.addView(this.mInflate, this.mParams);
                View view = this.mInflate;
                if (view != null) {
                    view.setVisibility(View.GONE);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public int dip2px(Context context, float f) {
        return (int) ((f * context.getResources().getDisplayMetrics().density) + 0.5f);
    }

    public void show(Context context, String str) {
        try {
            View view = this.mInflate;
            if (view != null) {
                view.setVisibility(View.VISIBLE);
            }
            if (!SDK.pause) {
                ImageView imageView = this.mPlaying;
                if (imageView != null) {
                    imageView.setVisibility(View.GONE);
                }
                ImageView imageView2 = this.mPause;
                if (imageView2 != null) {
                    imageView2.setVisibility(View.VISIBLE);
                }
            } else {
                ImageView imageView3 = this.mPlaying;
                if (imageView3 != null) {
                    imageView3.setVisibility(View.VISIBLE);
                }
                ImageView imageView4 = this.mPause;
                if (imageView4 != null) {
                    imageView4.setVisibility(View.GONE);
                }
            }
            TextView textView = this.mText;
            if (textView != null) {
                textView.setText(str);
            }
            if (this.mNum != null) {
                int i = SDK.nowNum + 1;
                int i2 = SDK.allNum;
                if (i >= i2) {
                    TextView textView2 = this.mNum;
                    textView2.setText("（" + i2 + "/" + i2 + "）");
                } else {
                    TextView textView3 = this.mNum;
                    textView3.setText("（" + i + "/" + i2 + "）");
                }
            }
            this.mWindowManager.updateViewLayout(this.mInflate, this.mParams);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void pause() {
        try {
            ImageView imageView = this.mPause;
            if (imageView != null) {
                imageView.setVisibility(View.GONE);
            }
            ImageView imageView2 = this.mPlaying;
            if (imageView2 != null) {
                imageView2.setVisibility(View.VISIBLE);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void resume() {
        try {
            ImageView imageView = this.mPlaying;
            if (imageView != null) {
                imageView.setVisibility(View.GONE);
            }
            ImageView imageView2 = this.mPause;
            if (imageView2 != null) {
                imageView2.setVisibility(View.VISIBLE);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void hide() {
        try {
            View view = this.mInflate;
            if (view != null) {
                view.setVisibility(View.GONE);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
